package cn.itcast.tanhua.server.controller;

import cn.hutool.core.collection.CollUtil;
import cn.itcast.tanhua.common.utils.UserThreadLocal;
import cn.itcast.tanhua.server.service.VideoService;
import cn.itcast.tanhua.server.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @ClassName VideoController
 * @Description 小视频相关接口
 * @Author WU
 * @Date 2021/6/2 19:09
 * @Version 1.0
 */
@RestController
@RequestMapping("smallVideos")
@Slf4j
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 发布小视频
     *
     * @param picFile
     * @param videoFile
     * @return org.springframework.http.ResponseEntity<java.lang.Void>
     */
    @PostMapping
    public ResponseEntity<Void> saveVideo(@RequestParam("videoThumbnail") MultipartFile picFile,
                                          @RequestParam("videoFile") MultipartFile videoFile) {
        try {
            Boolean bool = this.videoService.saveVideo(picFile, videoFile);
            if (bool) {
                //            返回true则发布成功
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("小视频发布接口，小视频发布失败，file= " + picFile.getOriginalFilename());
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询视频列表接口
     *
     * @param page
     * @param pageSize
     * @return org.springframework.http.ResponseEntity<cn.itcast.tanhua.server.vo.PageResult>
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
//        校验前端传来的页面
        if (page <= 0) {
            page = 1;
        }
        try {
//        调用service层方法查询视频列表
            PageResult pageResult = this.videoService.queryVideoList(page, pageSize);
//        如果视频列表不为空，则返回查询成功
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
//            查询失败记录日志
            log.error("查询小视频列表接口失败，userId= " + UserThreadLocal.get().getId(), e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /**
     * 小视频点赞接口
     *
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String videoId) {
        try {
//            点赞后返回视频点赞数量
            Long likeCount = this.videoService.likeComment(videoId);
            if (null != likeCount) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
//            失败则记录日志
            log.error("小视频点赞失败，videoId= " + videoId, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消小视频点赞接口
     *
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String videoId) {
        try {
            //            取消点赞后返回视频点赞数量
            Long likeCount = this.videoService.disLikeComment(videoId);
            if (null != likeCount) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
//            失败则记录日志
            log.error("小视频取消点赞失败，videoId= " + videoId, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 分页查询视频评论列表接口
     *
     * @param videoId
     * @return org.springframework.http.ResponseEntity<cn.itcast.tanhua.server.vo.PageResult>
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> queryCommentsList(@PathVariable("id") String videoId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = this.videoService.queryCommentList(videoId, page, pageSize);
            if (CollUtil.isNotEmpty(pageResult.getItems())) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询视频评论列表失败，videoId: " + videoId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 视频下发布评论接口
     *
     * @param param
     * @param videoId
     * @return org.springframework.http.ResponseEntity<java.lang.Void>
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> saveComment(@RequestBody Map<String, String> param,
                                            @PathVariable("id") String videoId) {
        try {
//        从集合中取出前端传来的参数
            String comment = param.get("comment");
            Boolean result = this.videoService.saveComment(videoId, comment);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("视频下发布评论失败，videoId= "+ videoId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 视频下评论点赞接口
     *
     * @param videoCommentId
     * @return org.springframework.http.ResponseEntity<java.lang.Long>
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("id") String videoCommentId) {
        try {
            Long likeCount = this.videoService.likeComment(videoCommentId);
            if (likeCount != null) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            log.error("视频下的评论点赞失败，videoCommentId= "+ videoCommentId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 视频下评论取消点赞接口
     *
     * @param videoCommentId
     * @return org.springframework.http.ResponseEntity<java.lang.Long>
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable("id") String videoCommentId) {
        try {
            Long likeCount = this.videoService.disLikeComment(videoCommentId);
            if (null != likeCount) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            log.error("视频下的评论取消点赞失败，videoCommentId= "+ videoCommentId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
